<template>
  <div id="register">
    <!--<video preload="auto" class="me-video-player" autoplay="autoplay" loop="loop">
          <source src="../../static/vedio/sea.mp4" type="video/mp4">
      </video>-->

    <div class="me-login-box me-login-box-radius">
      <h1>找帮帮 注册</h1>

      <el-form ref="userForm" :model="userForm" :rules="rules">
        <el-select v-model="userForm.schoolId" @change="HandReqCampus"  placeholder="请选择学校">
          <el-option  v-for="s in schoolList" :key="s.id" :label="s.schoolName" :value="s.id"></el-option>
        </el-select>
        <el-select v-if="userForm.schoolId" v-model="userForm.campusId" placeholder="请选择校区">
          <el-option v-for="c in campusList" :key="c.id" :label="c.schoolName" :value="c.id"></el-option>
        </el-select>

        <el-form-item prop="studentId">
          <el-input placeholder="学号" v-model="userForm.studentId"></el-input>
        </el-form-item>

        <el-form-item prop="studentName">
          <el-input placeholder="姓名" v-model="userForm.studentName"></el-input>
        </el-form-item>

        <el-form-item prop="phoneNo">
          <el-input placeholder="手机号" v-model="userForm.phoneNo"></el-input>
        </el-form-item>


        <el-form-item prop="userName">
          <el-input placeholder="用户名" v-model="userForm.userName"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="userForm.password" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item size="small" class="me-login-button">
          <el-button type="primary" @click.native.prevent="register('userForm')">注册</el-button>
        </el-form-item>
      </el-form>



    </div>
  </div>
</template>

<script>
import {reqCampusList, reqSchoolList} from "@/api";

export default {
  name: "RegisterIndex",
  data() {
    return {
      fullscreenLoading: false,
      schoolList:[],
      campusList:[],
      userForm: {
        userName: undefined,
        studentName: undefined,
        password: undefined,
        phoneNo: undefined,
        schoolId: undefined,
        campusId: undefined,
        studentId: undefined
      },
      rules: {
        userName: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {max: 10, message: '不能大于10个字符', trigger: 'blur'}
        ],
        studentName: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
          {max: 10, message: '不能大于10个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {max: 10, message: '不能大于10个字符', trigger: 'blur'}
        ],
        phoneNo: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {max: 11, message: '不能大于21个字符', trigger: 'blur'}
        ],
        schoolId: [
          {required: true, message: '请选择学校', trigger: 'blur'},
        ],
        campusId: [
          {required: true, message: '请选择校区', trigger: 'blur'},
        ],
        studentId: [
          {required: true, message: '请输入学号', trigger: 'blur'},
          {max: 21, message: '不能大于21个字符', trigger: 'blur'}
        ]
      },
    }
  },
  methods:{
    HandReqCampus(){
      reqCampusList(this.userForm.schoolId).then((res)=>{
        this.userForm.campusId = ''
        this.campusList = res
      })
    },
    //注册

     register(formName) {
      let that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const loading = that.$loading({
                lock: true,
                text: '注册中',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
              });
          that.$store.dispatch('ReqRegister',that.userForm).then(() => {
            loading.close();
            that.$message({message: '注册成功', type: 'success', showClose: true});
            that.$router.push({path: '/login'})
          }).catch((error) => {
            loading.close();
            that.$message({message: error.data.errorMsg, type: 'error', showClose: true});
          })

        } else {
          return false;
        }
      });

    }

  },
  mounted() {
  reqSchoolList().then((res)=>{
    this.schoolList = res
    console.log(this.schoolList)
  })
  },
}
</script>

<style scoped>

#login {
  min-width: 100%;
  min-height: 100%;
}

.me-video-player {
  background-color: transparent;
  width: 100%;
  height: 100%;
  object-fit: fill;
  display: block;
  position: absolute;
  left: 0;
  z-index: 0;
  top: 0;
}

.me-login-box {
  position: absolute;
  width: 300px;
  height: 320px;
  background-color: white;
  margin-top: 150px;
  margin-left: -180px;
  left: 50%;
  padding: 30px;
}

.me-login-box-radius {
  border-radius: 10px;
  box-shadow: 0px 0px 1px 1px rgba(161, 159, 159, 0.1);
}

.me-login-box h1 {
  text-align: center;
  font-size: 24px;
  margin-bottom: 20px;
  vertical-align: middle;
}

.me-login-design {
  text-align: center;
  font-family: 'Open Sans', sans-serif;
  font-size: 18px;
}

.me-login-design-color {
  color: #5FB878 !important;
}

.me-login-button {
  text-align: center;
}

.me-login-button button {
  width: 100%;
}
</style>
